/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: ManagerServiceImpl
 * Author:   wl
 * Date:     2018/3/19 11:27
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.example.mam.service.impl;


import com.example.mam.constant.Constant;
import com.example.mam.entity.Manager;
import com.example.mam.exception.BizException;
import com.example.mam.exception.BizExceptionEnum;
import com.example.mam.mapper.ManagerMapper;
import com.example.mam.service.ManagerService;
import com.example.mam.util.Condition;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Resource
    private ManagerMapper managerMapper;

    @Override
    public Long addManager(Manager manager) {
        Manager tmp = managerMapper.selectManagerByEmail(manager.getEmail());
        if (null != tmp)
            throw new BizException(BizExceptionEnum.EMAIL_REPEAT_ERROR);
        Long currentTime = System.currentTimeMillis();
        manager.setCreateTime(new java.sql.Timestamp(currentTime));
        manager.setUpdateTime(new java.sql.Timestamp(currentTime));
        manager.setStatus(Constant.YXBJ.YXBJ_YES);
        if (managerMapper.insert(manager) != 1) {
            throw new BizException(BizExceptionEnum.DB_INSERT_RESULT_ERROR);
        }
        return manager.getId();
    }

    @Override
    public Integer deleteManagerById(Long id) {
        int i = managerMapper.deleteByPrimaryKey(id);
        if (i != 1) {
            throw new BizException(BizExceptionEnum.DB_DELETE_RESULT_ERROR);
        }
        return i;
    }


    @Override
    public Manager getManagerById(Long id) {
        return managerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateManager(Manager manager) {
        return managerMapper.updateByPrimaryKeySelective(manager);
    }

    @Override
    public Manager getManagerByEmail(String email) {
        return managerMapper.selectManagerByEmail(email);
    }

    @Override
    public Integer freezeManagerAccount(Long id) {
        Manager manager = managerMapper.selectByPrimaryKey(id);
        if (null != manager) {
            manager.setStatus(Constant.YXBJ.YXBJ_NO);
            manager.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            managerMapper.updateByPrimaryKeySelective(manager);
            return 1;
        }
        return 0;
    }

    @Override
    public Integer thawManagerAccount(Long id) {
        Manager manager = managerMapper.selectByPrimaryKey(id);
        if (null != manager) {
            manager.setStatus(Constant.YXBJ.YXBJ_YES);
            manager.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            managerMapper.updateByPrimaryKey(manager);
            return 1;
        }
        return 0;
    }


    @Override
    public Integer restPassword(Manager manager) {
        if (manager == null || manager.getId() == 0) return 0;
        Manager tmp = managerMapper.selectByPrimaryKey(manager.getId());
        if (null != tmp && tmp.getId() != 0) {
            tmp.setEmail(manager.getEmail());
            tmp.setPassword(manager.getPassword());
            return managerMapper.updateByPrimaryKeySelective(tmp);
        }
        return 0;
    }

    @Override
    public List<Object> selectManagerByCondition(Condition condition) {
        condition.setTotal(managerMapper.selectResultCount());
        return managerMapper.selectManagerByExample(condition);
    }


}
